package algorithm.sorting.compare.selectsort;

import algorithm.sorting.ISort;

public class SelectSorter<T extends Comparable<T>> implements ISort<T> {

	public SelectSorter(){
		
	}
	
	@Override
	public void sort(T[] arr) {
		sort(arr,0,arr.length-1);
	}

	@Override
	public void sort(T[] arr, int head, int end) {
		if(arr.length==0 || head<0 || end>=arr.length || end<=head)
			return;
		for(int i=end;i>head;i--){
			int maxIdx = head;
			for(int j=head+1;j<=i;j++){
				if(arr[maxIdx].compareTo(arr[j])<0)
					maxIdx = j;
			}
			swap(arr,maxIdx,i);
		}
	}
	
	private void swap(T[] arr,int i,int j){
		T temp = arr[i];
		arr[i] = arr[j];
		arr[j] = temp;
	}
}
